var isRemember = true;
function initPage() {
	window.onresize = onResize;

	
	document.getElementById("registerPage").style.display='none';
	
	onResize();
		$("#remember")[0].onclick = onCheck;
		$("#rememberText")[0].onclick = onCheck;
	var type = getUrlParam("type");
	if(type == 1){
		alert("邮箱激活成功!");
	}
}


function onResize() {
	console.log("onResize");
	var viewHeight = $("#viewPanel").height();
	if (viewHeight <= 660) {
		viewHeight = 660;
	}
	$("#login_effect").height(0.79*viewHeight);
	$("#login_dialog").height(0.67*viewHeight);
	$("#registerPage").height(0.67*viewHeight);
	$("#registerPage").width(0.955*0.67*viewHeight);
	$("#login_dialog").width(0.955*0.67*viewHeight);
	var edge = 0.061*viewHeight;
	$("#login_panel").css({top:(edge)+"px",left:(edge)+"px",right:(edge)+"px",bottom:(edge)+"px"});
	$("#InRegister").css({top:(edge)+"px",left:(edge)+"px",right:(edge)+"px",bottom:(edge)+"px"});
};

function onCheck() {
	isRemember = !isRemember;
	if (isRemember) {
		$("#remember")[0].className = "gopt_checkbox_on";
	} else {
		$("#remember")[0].className = "gopt_checkbox_off";
	}
};

function checkKeyUp(e){
    var key = e.keyCode||e.charCode;
    if(key == 13){
        login();
    }
};
function login(){
	var loginType=getQueryString("loginType");
    var $u = $("#j_username"),
        $p = $("#j_password");
    var user = $u.val().trim();
    var pass = $p.val();
    if(user == ""){
        return info("请输入用户名!", $u);
    }
    if(!pass){
        return info("请输入密码!", $p);
    }

    var result = null;
	$.ajax({
		async: false,
		dataType: "json",
		data: {username:user,password:pass},
		url: "../pv/login/getUser"
	}).done(function(res) {
		result = res;
		if(result.flag == 0){
			var flag = sessionStorage.getItem("indexType");
			if(flag =='old'){
				window.parent.location.href = "../index.html?userId="+result.result;

			}else{
				window.parent.location.href = "../index.html?userId="+result.result;
			}
		}else{
	    	return info(result.result);
		}
	});
}
function encrypt(m, k, i){
    var C = CryptoJS, F = C.enc.Utf8.parse,
        h = C.MD5(m), hm = h+m,  
        key = F(k), iv  = F(i), srcs = F(hm);
    var v = C.AES.encrypt(srcs, key, { iv: iv,mode:C.mode.CBC});
    return v.toString();
}

function info(s, $e){
    $("#msg").text(s);
    if($e)$e.focus();
}

function setCookie(name,value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)) {
		return unescape(arr[2]);
	} else {
		return null;
	}
}
//前往免费注册页面功能
function toRegister(){
	document.getElementById("registerPage").style.display='block';
	document.getElementById("login_dialog").style.display='none';
}
//返回登录功能
function backLogin(){
	resetting();
	document.getElementById("login_dialog").style.display='block';
	document.getElementById("registerPage").style.display='none';
}
//重置信息功能
function resetting(){
	window.frames["registerIframe"].contentDocument.getElementById("emailInput").value="";
	window.frames["registerIframe"].contentDocument.getElementById("passwordInput").value="";
	window.frames["registerIframe"].contentDocument.getElementById("repasswordInput").value="";
	window.frames["registerIframe"].contentDocument.getElementById("renameInput").value="";
}
//免费注册功能
function register(){
	var email = window.frames["registerIframe"].contentDocument.getElementById("emailInput").value;
	var password = window.frames["registerIframe"].contentDocument.getElementById("passwordInput").value;
	var repassword = window.frames["registerIframe"].contentDocument.getElementById("repasswordInput").value;
	var rename = window.frames["registerIframe"].contentDocument.getElementById("renameInput").value;
	var msgEmail = window.frames["registerIframe"].contentDocument.getElementById("msgEmail").innerHTML;
	var msgRename = window.frames["registerIframe"].contentDocument.getElementById("msgRename").innerHTML;
	var msgPassword = window.frames["registerIframe"].contentDocument.getElementById("msgPassword").innerHTML;
	var msgRepassword = window.frames["registerIframe"].contentDocument.getElementById("msgRepassword").innerHTML;
	if(msgEmail == "" && msgRename == "" && msgPassword == "" && msgRepassword == "" &&
			!JyyCommon.isEmpty(email) && !JyyCommon.isEmpty(password) && !JyyCommon.isEmpty(repassword) && !JyyCommon.isEmpty(rename)){
		JyyLoading.start();
		$.ajax({
			type:"POST",
			url:"../pv/login/register",
			data:{"email":email,"username":rename,"password":password},
			success:function(res){
				msg = res.msg;
	    		flag = res.flag;
//	        	JyyMsgBox.alert({ title:"提示", message:msg,type:"warning"}).on(function (e) {});
	    		alert(msg);
				JyyLoading.end();
				window.location.href = "../pages/login.html";
			}
		})
	}else{
		alert("信息未正确填写!");
	}
}
//电子邮箱输入框判断
function onEmailInput(){
	if($("#emailInput").val() == null || $("#emailInput").val() == ""){
			$("#msgEmail").html("电子邮箱不可为空!");
		}else{
			var email = $("#emailInput").val();
			//email正则表达式
			var ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(!ePattern.test(email)){
				$("#msgEmail").html("输入电子邮箱格式不正确!");
		}else{
			$.ajax({
				type:"POST",
				url:"../pv/login/getEmail",
				data:{"email":email},
				success:function(res){
					msg = res.msg;
		    		flag = res.flag;
		    		if(flag == 0){
		    			$("#msgEmail").html("");
		    		}else{
		    			$("#msgEmail").html("该邮箱已被占用!");
		    		}
				}
			})
		}
	}
}
//确认密码输入框判断
function onRepasswordInput(){
	if($("#passwordInput").val() != null && $("#passwordInput").val() != "" && $("#repasswordInput").val() != null && $("#repasswordInput").val() != ""){
		if($("#passwordInput").val() == $("#repasswordInput").val()){
			$("#msgRepassword").html("");
			$("#msgPassword").html("");
		}else{
			$("#msgRepassword").html("两次密码输入不一致!");
		}
	}else {
		if($("#repasswordInput").val() == null || $("#repasswordInput").val() == ""){
			$("#msgRepassword").html("确认密码不可为空!");
		}else{
			$("#msgRepassword").html("");
		}
	}
}
//密码输入框判断
function onPasswordInput(){
	if($("#passwordInput").val() != null && $("#passwordInput").val() != "" && $("#repasswordInput").val() != null && $("#repasswordInput").val() != ""){
		if($("#passwordInput").val() == $("#repasswordInput").val()){
			$("#msgPassword").html("");
			$("#msgRepassword").html("");
		}else{
			$("#msgPassword").html("两次密码输入不一致!");
		}
	}else{
		if($("#passwordInput").val() == null || $("#passwordInput").val() == ""){
			$("#msgPassword").html("密码不可为空!");
		}else{
			$("#msgPassword").html("");
		}
	} 
}
//昵称输入框判断
function onRenameInput(){
	if($("#renameInput").val() == null || $("#renameInput").val() == ""){
		$("#msgRename").html("昵称不可为空!");
	}else{
		$("#msgRename").html("");
	}
}


function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null){
    	return unescape(r[2]);
    } else{
    	return null;
    } 
}

function goIndexPage(){ 
	window.parent.location.href = "../../index.html";
}


function goLoginPage(){
	window.parent.location.href="login.html";
}
//获取url中参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return r[2];
    return null;
}